package com.pzhu.user.dao;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.pzhu.base.common.utils.mybatisflex.LogicDeleteService;
import com.pzhu.base.common.utils.mybatisflex.QueryUtils;
import com.pzhu.user.domain.entity.User;
import com.pzhu.user.domain.ro.UserSearchRO;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

import static com.pzhu.user.domain.entity.table.UserTableDef.USER;

/**
 * @author 75073
 * @description 针对表【user(用户表)】的数据库操作Mapper
 * @createDate 2024-02-19 00:37:22
 */
public interface UserDAO extends LogicDeleteService<User> {
    /**
     * 用户名计数
     *
     * @param username 用户名
     * @return long
     *
     */
    default boolean existByUserName(String username) {
        if (StringUtils.isBlank(username)) {
            return false;
        }
        QueryCondition condition = QueryUtils.and(USER.USERNAME.like(username), QueryUtils.del(USER.DELETED_ID));
        return exists(condition);
    }

    default Page<User> page(UserSearchRO userSearchRo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        QueryCondition condition = QueryUtils.and(
                QueryUtils.del(USER.DELETED_ID),
                USER.USERNAME.like(userSearchRo.getUsername()),
                USER.PHONE.eq(userSearchRo.getPhone()));
        queryWrapper.where(condition);
        queryWrapper.orderBy(USER.ID, false);
        return page(userSearchRo, queryWrapper);
    }

    default boolean existByUserId(Long userId) {
        if (userId == null) {
            return false;
        }
        QueryCondition condition = QueryUtils.and(USER.ID.eq(userId), QueryUtils.del(USER.DELETED_ID));
        return exists(condition);
    }

    default User selectByUserId(Long userId) {
        if (Objects.isNull(userId)) {
            return null;
        }
        return selectOneById(userId);
    }

    default User selectOneByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return selectOneByCondition(QueryUtils.and(QueryUtils.del(USER.DELETED_ID), USER.USERNAME.eq(username)));
    }
}
